###############################################################################
## The Tucan Project - Report Nest
##
## Copyright (C) 2009 Fran Lupion crak@tucaneando.com
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
###############################################################################


import os
import urllib
import cgi

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

POST = """
<html>
<head>
<title>report test.</title>
</head>
<body>
<form name="report" method="post" action="/add">
<input type="text" maxlength="36" size="36" name="uuid" value="" />
<input type="text" maxlength="24" size="24" name="email" value="" />
<textarea name="comment" cols="80" rows="10"></textarea>
<textarea name="log" cols="80" rows="10"></textarea>
<input type="submit" name="send" value="send"/>
</form>
</body>
</html>
"""

QUERY = """
<html>
<head>
<title>Query Report.</title>
</head>
<body>
<form name="query" method="post" action="/query">
<input type="text" maxlength="32" size="32" name="key" value="" />
<input type="submit" name="check" value="check"/>
</form>
</body>
</html>
"""

class QueryDB(db.Model):
	""""""
	date = db.DateTimeProperty(auto_now=True)
	uuid = db.StringProperty()
	email = db.StringProperty()
	comment = db.TextProperty()
	log = db.TextProperty()

class MainPage(webapp.RequestHandler):
	""""""
	def get(self):
		""""""
		self.response.headers['Content-Type'] = 'text/html'
		#self.response.out.write(POST)
		self.response.out.write("LOG REPORT!")

class Query(webapp.RequestHandler):
	""""""
	def get(self):
		""""""
		self.response.headers['Content-Type'] = 'text/html'
		self.response.out.write(QUERY)

	def post(self):
		""""""
		self.response.headers['Content-Type'] = 'text/html'
		if "key" in self.request.POST.keys():
			try:
				query = QueryDB.get(self.request.POST["key"])
			except:
				self.response.out.write("Not a valid key!")
			else:
				if query:
					template_values = {
							'key': self.request.POST["key"],
							'date': query.date,
							'uuid': query.uuid,
							'email': query.email,
							'comment': query.comment.split("\n"),
							'log': query.log.split("\n"),
							}
					path = os.path.join(os.path.dirname(__file__), 'query.html')
					self.response.out.write(template.render(path, template_values))
				else:
					self.response.out.write("Not Found!")

class Add(webapp.RequestHandler):
	""""""
	def post(self):
		""""""
		self.response.headers['Content-Type'] = 'text/plain'
		try:
			uuid = self.request.POST["uuid"]
			email = self.request.POST["email"]
			comment = urllib.unquote(self.request.POST["comment"])
			log = urllib.unquote(self.request.POST["log"])
		except Exception, e:
			self.response.out.write(e)
		else:
			query = QueryDB(uuid=uuid, email=email, comment=comment, log=log)
			query.put()
			self.response.out.write(query.key())

if __name__ == "__main__":
	run_wsgi_app(webapp.WSGIApplication([('/', MainPage), ('/add', Add), ('/query', Query)], debug=True))